﻿import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtMultimedia 5.9
import QtQuick.Layouts 1.0

import BCQmlRenderView 1.0
import BCDevice 1.0
import QtQuick.Controls 1.4

Window {
    id:root
    width: 960
    height: 480
    visible: true
    title: qsTr("media")

    signal startInput(int index)
    signal startOutput(int index)
    signal startCamera(int index)

    signal stopAll();


    ListModel {
        id:m_cameraModel

    }

    RowLayout {
        anchors.fill: parent

        BCQmlRenderView {
            id:m_videoWidget
            width: 320
            height: 240
            visible: true
        }

        ColumnLayout {
            Label {
                text: "麦克风："
            }

            ComboBox {
                id:m_cbMicrophone
                Layout.fillWidth: true
                model: BCDevice.getMicrophones()
            }

            Label {
                text: "扬声器："
            }

            ComboBox {
                id:m_cbLoudspeaker
                Layout.fillWidth: true
                model:BCDevice.getLoudspeakers()
            }

            Label {
                text: "摄像头："
            }

            ComboBox {
                id:m_cbCamera
                Layout.fillWidth: true
                model:BCDevice.getCameras()
            }

            GridLayout {
                columns: 3
                Button {
                    id : m_btnVoice
                    objectName: "Button"
                    text: "启动"
                }

                Button {
                    id:m_btnStop
                    text: "暂停"
                }

                Button {
                    id:m_btnRefresh
                    text: "刷新"
                }

                Button {
                    id:m_btnPlayMp3
                    text: "MP3"
                }

                Button {
                    id:m_btnStopMp3
                    text: "停止MP3"
                }

            }
        }
    }

    function updateDev()
    {
        m_cbCamera.model = BCDevice.getCameras();
        m_cbLoudspeaker.model = BCDevice.getLoudspeakers();
        m_cbMicrophone.model = BCDevice.getMicrophones();

        if (m_cbCamera.model.length === 0)
            m_cbCamera.currentIndex = -1
        if (m_cbLoudspeaker.model.length === 0)
            m_cbLoudspeaker.currentIndex = -1;
        if (m_cbMicrophone.model.length === 0)
            m_cbMicrophone.currentIndex = -1;
    }

    Connections {
        target: m_btnVoice
        onClicked:{
            console.log("thread:",BCDevice.currentThreadId());
            console.log("microphone",m_cbMicrophone.currentIndex);
            console.log("loudspeaker",m_cbLoudspeaker.currentIndex);
            console.log("camera",m_cbCamera.currentIndex);

            startInput(m_cbMicrophone.currentIndex);
            startOutput(m_cbLoudspeaker.currentIndex);
            startCamera(m_cbCamera.currentIndex);
        }
    }

    Connections {
        target: m_btnPlayMp3
        onClicked:{
                                    BCDevice.playMp3("D:\\TestDeviceMusic.mp3");//8000
//                        BCDevice.playMp3("D:\\sax.mp3"); // 8000
//                                    BCDevice.playMp3("D:\\skycity1.mp3");//44100
//                        BCDevice.playMp3("D:\\abc.mp3"); // 44100
//            BCDevice.playMp3("D:\\bgm_t1.mp3");
        }
    }

    Connections {
        target: m_btnStopMp3
        onClicked:{
            BCDevice.stopMp3();
        }
    }

    Connections {
        target: m_btnRefresh
        onClicked: {
            stopAll();

            updateDev();
        }
    }

    Connections {
        target: m_btnStop
        onClicked: {
            stopAll();
        }
    }

    Connections {
        target: event
        onDevUpdate:{
            updateDev();
            console.log("更新设备");
        }
    }

    Component.onCompleted: {
        generate.setAVideoWidget(m_videoWidget);
        BCDevice.setAudioParam(44100,2,16);
        console.log("in qml BCDevice thread:",BCDevice.currentThreadId());
    }
}
